package com.weather.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import com.weather.service.RequestWeatherDay;
import com.weather.service.SelectSql;

/**
 * Servlet implementation class RequestWeatherServlet
 */
public class RequestWeatherServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RequestWeatherServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	@SuppressWarnings("null")
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		HttpSession session = request.getSession();
	    List<String> fileName=new ArrayList<String>();
	   // String[] file=new String[3];
     	try {
			JSONObject weathejson = RequestWeatherDay.RequestWeatherOneDay();
			String date = weathejson.getString("time").replaceAll("T", " ");
			String wind = weathejson.getString("WD")+" "+weathejson.getString("WS");
            String temp = weathejson.getString("temp");
			
            System.out.println(temp.substring(0, 1));
            System.out.println(temp.substring(1, 2));
            //fileName.add(date);
            //fileName.add(wind);
            for (int i=0;i<temp.length();i++){
            		int str=Integer.parseInt(temp.substring(i,i+1));
            		
            		System.out.println(getfileName(str));
                  if (getfileName(str) != null){
                    	fileName.add(getfileName(str));
                    }
                // file[i]=getfileName(str);
            		
            }
         System.out.println(fileName.toString());
        // System.out.println(file.toString());
         session.setAttribute("datetime", date);
         session.setAttribute("dwind", wind);
         session.setAttribute("file1", "Resources/"+fileName.get(0));
         session.setAttribute("file2", "Resources/"+fileName.get(1));
         response.setContentType("application/json; charset=utf-8");
            
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
     	
/*     	request.getRequestDispatcher("/NewFile.jsp").forward(
				request, response);
     	System.out.println(request.getContextPath());*/
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//String reqValue=request.getAttribute("");
		String sql="select distinct city from weather_";
		response.setContentType("application/json; charset=utf-8");
		HttpSession session = request.getSession();
		try {
			List<String> cityList = SelectSql.SelectValueOneArray(sql);
			System.out.println(cityList.toString());
			response.getWriter().write(cityList.toString());
			session.setAttribute("cityList", cityList);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	public static String getfileName(int value){
        String str="";
		switch (value){
		case 0:
			str="0.png";
			break;
		case 1:
			str="1.png";
			break;
		case 2:
			str="2.png";
			break;
		case 3:
			str="3.png";
			break;
		case 4:
			str="4.png";
			break;
		case 5:
			str="5.png";
			break;
		case 6:
			str="6.png";
			break;
		case 7:
			str="7.png";
			break;
		case 8:
			str="8.png";
			break;
		case 9:
			str="9.png";
			break;
		default : str="0_.png"; 
		break; 	
		}
		return str;
	}
}
